import axios from 'axios';
let token = {
    state: {
        phone: '',
        isok: true
    },
    mutations: {
        login(state, {
            phone,
            token
        }) { //登陆成功
            state.phone = {
                phone,
                token
            }
            //存token
            localStorage.setItem("token", token);
        },
        logout(state) { //退出
            state.phone = '';
            localStorage.removeItem("token");
        }
    },
    actions: {
        async verify(contex) { //contex就是store   == this.$store.commit
            let token = localStorage.getItem("token");
            if (token) {
                let {
                    data
                } = await axios.get("http://localhost:5555/listbo/verify",  {
                    params: {
                        token
                    }
                })
                if (data.type) { //成功
                    contex.state.isok = true;
                } else { //失败
                    contex.commit('logout');
                    contex.state.isok = false
                }
            }
        }
    }
}



export default token;